package dao;
import java.sql.*;
import pojo.Login;
public class LoginDAO {
    static final String JDBC_DRIVER="com.mysql.cj.jdbc.Driver";
    static final String DB_URL="jdbc:mysql://localhost:3306/summer_design";
    static final String USER="root";
    static final String PASS="kangsiyuan516@";
    Connection conn=null;
    Statement stmt=null;
    public int checkLogin(Login loginform){
        String name=loginform.getUsername();
        String password=loginform.getPassword();
        int role=loginform.getRole();//id为1的是管理员
        try{
            Class.forName(JDBC_DRIVER);
            conn=DriverManager.getConnection(DB_URL,USER,PASS);
            stmt=conn.createStatement();
            String sql="SELECT * FROM user";
            ResultSet rs=stmt.executeQuery(sql);
            while(rs.next()){
                int userid=rs.getInt("role");//0=role 为管理员
                String username=rs.getString("name");
                String pass=rs.getString("password");
                if(role==0){//管理员登录
                    if(name.equals(loginform.getUsername())){
                        if(!password.equals(loginform.getPassword())){
                            return 1;//登录失败
                        }else{
                            return 2;//管理员登录成功
                        }
                    }
                }else{
                    if(name.equals(loginform.getUsername())){
                        if(!password.equals(loginform.getPassword())){
                            return 1;//登录失败
                        }else{
                            return 3;//用户登录成功
                        }
                    }
                }

            }
        }
        catch (Exception e){
            e.printStackTrace();
        }
        return 0;//没有此人
    }
}
